package com.wuwei.aliocr.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.wuwei.aliocr.domain.LessonRecord;
import com.wuwei.aliocr.listener.LessonRecordListener;
import com.wuwei.aliocr.listener.LessonRecordDao;
import com.wuwei.aliocr.service.UploadService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class UploadServiceImpl implements UploadService {

    @Resource
    private LessonRecordDao lessonRecordDao;

    @Override
    @SneakyThrows
    public void uploadToSql(String filename) {
        // 读取全部sheet
//        EasyExcel.read(filename, LessonRecord.class, new LessonRecordListener(workdayDao)).doReadAll();
        //需要读取EXCEL工作簿的数量，如果sheet数量不等于6，则抛出异常



//        try(ExcelReader excelReader = EasyExcel.read(filename).build()){
//            ReadSheet readSheet1 =
//                    EasyExcel.readSheet(0).head(LessonRecord.class).registerReadListener(new LessonRecordListener(lessonRecordDao)).build();
//            ReadSheet readSheet2 =
//                    EasyExcel.readSheet(1).head(LessonRecord.class).registerReadListener(new LessonRecordListener(lessonRecordDao)).build();
//            ReadSheet readSheet3 =
//                    EasyExcel.readSheet(2).head(LessonRecord.class).registerReadListener(new LessonRecordListener(lessonRecordDao)).build();
//            ReadSheet readSheet4 =
//                    EasyExcel.readSheet(3).head(LessonRecord.class).registerReadListener(new LessonRecordListener(lessonRecordDao)).build();
//            ReadSheet readSheet5 =
//                    EasyExcel.readSheet(4).head(LessonRecord.class).registerReadListener(new LessonRecordListener(lessonRecordDao)).build();
//            ReadSheet readSheet6 =
//                    EasyExcel.readSheet(5).head(LessonRecord.class).registerReadListener(new LessonRecordListener(lessonRecordDao)).build();
//            ReadSheet readSheet7 =
//                    EasyExcel.readSheet(6).head(LessonRecord.class).registerReadListener(new LessonRecordListener(lessonRecordDao)).build();
//            excelReader.read(readSheet1,readSheet2,readSheet3,readSheet4,readSheet4,readSheet5,readSheet6,readSheet7);
//        }

        try(ExcelReader excelReader = EasyExcel.read(filename).build()){
            ReadSheet readSheet1 =
                    EasyExcel.readSheet(0).head(LessonRecord.class).registerReadListener(new LessonRecordListener(lessonRecordDao)).build();
            excelReader.read(readSheet1);
        }
    }
}
